import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class DeadLockDemo {

    public static Object resource1 = new Object();
    public static Object resource2 = new Object();

    public static void main(String[] args) {
        new Thread(() -> {
            synchronized (resource1){
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"拿着resource1，想要resource2");
                synchronized (resource2){
                    System.out.println(Thread.currentThread().getName()+"拿到了resource2");
                }
            }
        },"A").start();


        new Thread(() -> {
            synchronized (resource2){
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"拿着resource2，想要resource1");
                synchronized (resource1){
                    System.out.println(Thread.currentThread().getName()+"拿到了resource1");
                }
            }
        },"B").start();
    }

}
